package com.yexiao.cofe.business.controller;

import cn.hutool.core.collection.CollectionUtil;
import com.yexiao.cofe.business.mapper.*;
import com.yexiao.cofe.business.pojo.*;
import com.yexiao.cofe.constant.ParamConstant;
import com.yexiao.cofe.constant.PermissionConstant;
import com.yexiao.cofe.util.SystemUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

@RestController
@RequestMapping("/init")
@Slf4j
public class InitController {

    @Resource
    private CategoryMapper categoryMapper;
    @Resource
    private ParamItemMapper paramItemMapper;
    @Resource
    private ParamValueMapper paramValueMapper;
    @Resource
    private StoreMapper storeMapper;
    @Resource
    private RoleMapper roleMapper;
    @Resource
    private PermissionMapper permissionMapper;

    @GetMapping
    public String init() {
        initCategory();
        initParam();
        initStore();
        initPermission();
        initRole();
        return "success";
    }

    private void initCategory() {
        log.info("初始化类别---------");
        categoryMapper.delete(null);
        Category category = new Category();
        category.setId(0);
        category.setLevel(0);
        category.setName("根节点");
        category.setParentId(-1);
        category.setSort(0);
        categoryMapper.insert(category);

        Category category1 = new Category();
        category1.setName("风味拿铁")
                .setSort(1)
                .setParentId(category.getId())
                .setLevel(1);
        categoryMapper.insert(category1);

        Category category2 = new Category();
        category2.setName("美式家族")
                .setSort(2)
                .setParentId(category.getId())
                .setLevel(1);
        categoryMapper.insert(category2);

        Category category3 = new Category();
        category3.setName("柠檬茶")
                .setSort(3)
                .setParentId(category.getId())
                .setLevel(1);
        categoryMapper.insert(category3);

        Category category4 = new Category();
        category4.setName("生椰家族")
                .setSort(4)
                .setParentId(category.getId())
                .setLevel(1);
        categoryMapper.insert(category4);

        Category category5 = new Category();
        category5.setName("轻轻茉莉")
                .setSort(5)
                .setParentId(category.getId())
                .setLevel(1);
        categoryMapper.insert(category5);

    }

    private void initParam() {
        log.info("初始化参数---------");
        paramItemMapper.delete(null);
        paramValueMapper.delete(null);

        ParamItem item1 = new ParamItem();
        item1.setName("甜度")
                .setSort(1)
                .setType(ParamConstant.Param_Type_Single) // 单选
                .setIsRequired(ParamConstant.Param_Necessary); // 必需
        paramItemMapper.insert(item1);

        ParamItem ice = new ParamItem();
        ice.setName("是否加冰")
                .setSort(2)
                .setType(ParamConstant.Param_Type_Single)
                .setIsRequired(ParamConstant.Param_Necessary);
        paramItemMapper.insert(ice);

        ParamItem item3 = new ParamItem();
        item3.setName("小料")
                .setSort(1)
                .setType(ParamConstant.Param_Type_Multiple) // 多选
                .setIsRequired(ParamConstant.Param_Not_Necessary); // 非必需
        paramItemMapper.insert(item3);

        ParamValue value1 = new ParamValue();
        value1.setValue("不加糖")
                .setParamItemId(item1.getId());
        paramValueMapper.insert(value1);

        ParamValue value2 = new ParamValue();
        value2.setValue("三分糖")
                .setParamItemId(item1.getId());
        paramValueMapper.insert(value2);

        ParamValue value3 = new ParamValue();
        value3.setValue("五分糖")
                .setParamItemId(item1.getId());
        paramValueMapper.insert(value3);

        ParamValue value4 = new ParamValue();
        value4.setValue("七分糖")
                .setParamItemId(item1.getId());
        paramValueMapper.insert(value4);

        ParamValue value5 = new ParamValue();
        value5.setValue("正常糖")
                .setParamItemId(item1.getId());
        paramValueMapper.insert(value5);

        ParamValue value6 = new ParamValue();
        value6.setValue("正常冰")
                .setParamItemId(ice.getId());
        paramValueMapper.insert(value6);

        ParamValue value7 = new ParamValue();
        value7.setValue("少冰")
                .setParamItemId(ice.getId());
        paramValueMapper.insert(value7);

        ParamValue value8 = new ParamValue();
        value8.setValue("常温")
                .setParamItemId(ice.getId());
        paramValueMapper.insert(value8);

        ParamValue value9 = new ParamValue();
        value9.setValue("脆啵啵")
                .setParamItemId(item3.getId());
        paramValueMapper.insert(value9);

        ParamValue value10 = new ParamValue();
        value10.setValue("香草")
                .setParamItemId(item3.getId());
        paramValueMapper.insert(value10);

        ParamValue value11 = new ParamValue();
        value11.setValue("珍珠")
                .setParamItemId(item3.getId());
        paramValueMapper.insert(value11);

        ParamValue value12 = new ParamValue();
        value12.setValue("芋泥")
                .setParamItemId(item3.getId());
        paramValueMapper.insert(value12);

        ParamValue value13 = new ParamValue();
        value13.setValue("芒果果粒")
                .setParamItemId(item3.getId());
        paramValueMapper.insert(value13);

        ParamValue value14 = new ParamValue();
        value14.setValue("芋泥")
                .setParamItemId(item3.getId());
        paramValueMapper.insert(value14);

        ParamValue value15 = new ParamValue();
        value15.setValue("柚子果粒")
                .setParamItemId(item3.getId());
        paramValueMapper.insert(value15);

    }

    /**
     * 初始化公共门店
     */
    private void initStore() {
        log.info("初始化门店---------");
        List<Store> list = storeMapper.listStore("公共门店");
        if(CollectionUtil.isNotEmpty(list)) {
            return;
        }

        Store store = new Store();
        String storeId = SystemUtil.uuid().substring(0, 9);
        store.setStoreId(storeId)
                .setName("公共门店")
                .setSort(0);
        storeMapper.insert(store);
    }

    /**
     * 初始化基础角色
     */
    private void initRole() {
        log.info("初始化角色---------");
        Role role = roleMapper.selectRoleByRoleName("员工");
        if(role == null) {
            role = new Role();
            role.setRoleName("员工");
            role.setSort(0);
            roleMapper.insert(role);
        }
    }

    /**
     * 初始化根权限
     */
    private void initPermission() {
        log.info("初始化权限---------");
        Permission root = permissionMapper.selectByPermissionId("root");
        if (root == null) {
            root = new Permission();
            root.setPermissionId("root");
            root.setPermissionName("根节点");
            root.setSort(0);
            permissionMapper.insert(root);
        }

        // 初始化菜单权限
        for (String permissionStr : PermissionConstant.permissionList) {
            Permission permission = permissionMapper.selectByPermissionId(permissionStr);
            if(permission == null) {
                permission = new Permission();
                permission.setPermissionId(permissionStr);
                permission.setPermissionName(permissionStr);
                permission.setFatherPermissionId(root.getPermissionId());
                permission.setFatherPermissionName(root.getPermissionName());
                permission.setSort(0);
                permissionMapper.insert(permission);
            }
        }
    }

}
